package com.scripts;

import Utilities.Commons;
//import com.sun.deploy.services.PlatformType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.URI;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by orlandocampos on 10/29/14.
 */
public class SeleniumServerBasico {

    protected static WebDriver wd;
    protected static String url;
    protected static WebElement searchBar;

    @BeforeClass
    public void setup() throws Exception{
        wd = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"),
                                            DesiredCapabilities.chrome());

        /*
        *
        * PARA CONFIGURAR EL TEXT Y QUE CORRA EN OTRA PLATAFORMA
        *
        * */

        wd = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"),
                new DesiredCapabilities("firefox","", Platform.WINDOWS));

        url = "https://www.google.com/";

    }

    @Test
    public void explicitWait() throws Exception {
        wd.get(url);

        searchBar = wd.findElement(By.id("gbqfq"));
        searchBar.sendKeys("Pluralsight");

        /////////////////////////////////////// Explicit WAIT

        (wd.findElement(By.id("gbqfb"))).click();

        WebElement myDynamicElement2 = (new WebDriverWait(wd, 10))
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".q.qs")));

        //////////////////////////////////////////////////////////////////////////////////

        (wd.findElements(By.cssSelector(".q.qs"))).get(2).click();


        WebElement myDynamicElement3 = (new WebDriverWait(wd, 10))
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector("#rg_s>div")));

        (wd.findElements(By.cssSelector("#rg_s>div"))).get(0).click();

        Commons.wait(Commons.TIMEOUT_5_SECONDS);

    }

    @AfterTest
    public void finish() {
        wd.close();
        wd.quit();
    }

    ////////  otro ejemplo!!!!
    /*
    private WebDriver driver;

    public void setUp() throws Exception {
        DesiredCapabilities abilities = DesiredCapabilities.firefox();
        capabillities.setCapability("version", "16");
        capabillities.setCapability("platform", Platform.WINDOWS);
        capabillities.setCapability("name", "Testing Selenium-2 Remote WebDriver");

        driver = new RemoteWebDriver( new URL("http://key:secret@hub.testingbot.com:4444/wd/hub"), abilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().setPosition(new Point(220, 10));
        driver.manage().window().setSize(new Dimension(1000,650));
    }

    public void testSimple() throws Exception {
        this.driver.get("http://www.google.com");
        assertEquals("Google", this.driver.getTitle());
    }

    public void tearDown() throws Exception {
        this.driver.quit();
    }
    */

}